package com.awifi.dbus.util.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
* Http请求工具类
*Title: HttpUtil.java
*Description: 接入系统平台类文件
*Copyright: 中国电信爱WiFI运营中心
*Company: 中国电信爱WiFI运营中心
*@author sucb
*@date 2017年6月7日下午5:12:03
*/
public final class HttpUtil {

    private HttpUtil(){

    }

    /**
     * 日志对象
     */
    private static Logger LOG = LoggerFactory.getLogger(HttpUtil.class);

    /**
     * 获取token
     * @param getTokenUrl 获取token的url（不包含参数部分）
     * @param oauthCode 权限中心给的第三方授权码
     * @param timeout
     * @return token
     * @author sucb
     * @date 2017年6月7日下午8:53:22
     */
    public static String getToken(String getTokenUrl, String oauthCode, int timeout) {
        String url = getTokenUrl +"?oauthCode="+ oauthCode;
        LOG.info("http请求获取token的url = {}.", url);
        OkHttpClient okHttpClient = new OkHttpClient.Builder().
                readTimeout(timeout,TimeUnit.SECONDS).
                build();

        Request request = new Request.Builder().url(url).build();

        Call call = okHttpClient.newCall(request);
        try {
        Response response = call.execute();
        String res = response.body().string();
        response.close();
        LOG.info("http请求获取token的返回结果res = {}.", res);
        return res;
        } catch (IOException e) {
            LOG.error("http请求获取token异常,e = {}.", e);
            return null;
        }
    }

    /**
     * 获取ac设备+虚拟信息
     * @param getEntityInfoUrl 获取ac设备+虚拟信息的url（不包括参数和token）
     * @param params 参数
     * @param token token
     * @param timeout
     * @return ac设备+虚拟信息
     * @author sucb
     * @date 2017年6月7日下午8:38:14
     */
    public static String getEntityInfoList(String getEntityInfoUrl,
                                           String params, String token, int timeout) {
        String url = getEntityInfoUrl + "?params=" + getURLEncoderString(params)+ "&access_token="+token;
        LOG.info("http请求获取ac设备及虚拟信息的url = {}.", url);
        OkHttpClient okHttpClient = new OkHttpClient.Builder().
                readTimeout(timeout,TimeUnit.SECONDS).
                build();

        Request request = new Request.Builder()
                .url(url).build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String res = response.body().string();
            response.close();
            LOG.info("http请求获取ac设备及虚拟信息的返回结果res = {}.", res);
            return res;
        } catch (IOException e) {
            LOG.error("http请求获取ac设备及虚拟信息异常,e = {}.", e);
            return null;
        }
    }

    /**
     * url转码
     * @param url url
     * @return 转码后的url
     * @author sucb
     * @date 2017年6月7日下午8:54:30
     */
    public static String getURLEncoderString(String url) {
        String result = "";
        LOG.info("需要转码的url = {}", url);
        if (null == url) {
            return "";
        }
        try {
            result = URLEncoder.encode(url, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            LOG.error("url转码异常,url = {},e = {}.", url, e);
        }
        return result;
    }

}
